package com.api.hbjsl.web;

import com.jsl.action.AddProjectActionProxy;
import com.jsl.action.OtherInboundActionProxy;
import com.jsl.action.OtherOutAndInActionProxy;
import com.jsl.action.OutboundRequestActionProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.ofs.interfaces.SendRequestStatusDataProxy;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

@Path("/hbjsl/hotswap")
public class HotSwapAction {
    private static final Logger log = LoggerFactory.getLogger(HotSwapAction.class);

    private final SendRequestStatusDataProxy hotSwapService = new SendRequestStatusDataProxy();

    private final AddProjectActionProxy projectActionProxy = new AddProjectActionProxy();

    private final OutboundRequestActionProxy outboundRequestActionProxy = new OutboundRequestActionProxy();

    private final OtherInboundActionProxy otherInboundActionProxy = new OtherInboundActionProxy();

    private final OtherOutAndInActionProxy otherOutAndInActionProxy = new OtherOutAndInActionProxy();


    /**
     * 手动触发热更新接口
     * @return 统一响应（成功/失败列表）
     */
    @Path("/trigger") // GET请求：http://localhost:8088/api/hbjsl/hotswap/trigger
    @Produces(MediaType.TEXT_PLAIN)
    @GET
    public String triggerHotSwap() {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("收到热更新触发请求...");
            // 调用服务层方法，获取更新结果

            result.put("status", "success");
            result.put("updateResult", hotSwapService.triggerHotSwap());
            result.put("msg", "详细日志请查看服务控制台");
            return result.toString();
        } catch (Exception e) {
            log.error("触发热更新接口异常", e);
            result.put("status", "fail");
            result.put("msg", "触发热更新失败：" + e.getMessage());
            return result.toString();
        }
    }

    @Path("/trigger/message") // GET请求：http://localhost:8088/api/hbjsl/hotswap/trigger/message
    @Produces(MediaType.TEXT_PLAIN)
    @GET
    public String triggerMessage() {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("收到热更新触发请求...");
            // 调用服务层方法，获取更新结果

            result.put("status", "success");
            result.put("updateResult", hotSwapService.triggerHotSwap());
            result.put("msg", "详细日志请查看服务控制台");
            return result.toString();
        } catch (Exception e) {
            log.error("触发热更新接口异常", e);
            result.put("status", "fail");
            result.put("msg", "触发热更新失败：" + e.getMessage());
            return result.toString();
        }
    }

    @Path("/trigger/project") // GET请求：http://localhost:8088/api/hbjsl/hotswap/trigger/project
    @Produces(MediaType.TEXT_PLAIN)
    @GET
    public String triggerProject() {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("收到热更新触发请求...");
            // 调用服务层方法，获取更新结果

            result.put("status", "success");
            result.put("updateResult", projectActionProxy.triggerHotSwap());
            result.put("msg", "详细日志请查看服务控制台");
            return result.toString();
        } catch (Exception e) {
            log.error("触发热更新接口异常", e);
            result.put("status", "fail");
            result.put("msg", "触发热更新失败：" + e.getMessage());
            return result.toString();
        }
    }

    @Path("/trigger/outboundRequest") // GET请求：http://localhost:8088/api/hbjsl/hotswap/trigger/outboundRequest
    @Produces(MediaType.TEXT_PLAIN)
    @GET
    public String triggerOutboundRequest() {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("收到热更新触发请求...");
            // 调用服务层方法，获取更新结果

            result.put("status", "success");
            result.put("updateResult", outboundRequestActionProxy.triggerHotSwap());
            result.put("msg", "详细日志请查看服务控制台");
            return result.toString();
        } catch (Exception e) {
            log.error("触发热更新接口异常", e);
            result.put("status", "fail");
            result.put("msg", "触发热更新失败：" + e.getMessage());
            return result.toString();
        }
    }

    @Path("/trigger/otherInbound") // GET请求：http://localhost:8088/api/hbjsl/hotswap/trigger/outboundRequest
    @Produces(MediaType.TEXT_PLAIN)
    @GET
    public String triggerOtherInbound() {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("收到热更新触发请求...");
            // 调用服务层方法，获取更新结果

            result.put("status", "success");
            result.put("updateResult", otherInboundActionProxy.triggerHotSwap());
            result.put("msg", "详细日志请查看服务控制台");
            return result.toString();
        } catch (Exception e) {
            log.error("触发热更新接口异常", e);
            result.put("status", "fail");
            result.put("msg", "触发热更新失败：" + e.getMessage());
            return result.toString();
        }
    }

    @Path("/trigger/otherOutAndIn") // GET请求：http://localhost:8088/api/hbjsl/hotswap/trigger/otherOutAndIn
    @Produces(MediaType.TEXT_PLAIN)
    @GET
    public String triggerOtherOutAndIn() {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("收到热更新触发请求...");
            // 调用服务层方法，获取更新结果

            result.put("status", "success");
            result.put("updateResult", otherOutAndInActionProxy.triggerHotSwap());
            result.put("msg", "详细日志请查看服务控制台");
            return result.toString();
        } catch (Exception e) {
            log.error("触发热更新接口异常", e);
            result.put("status", "fail");
            result.put("msg", "触发热更新失败：" + e.getMessage());
            return result.toString();
        }
    }
}
